Ext.define('FLookup', {
    extend: 'Ext.form.FieldContainer',
    alias: 'widget.flookup',    
    fieldLabel: '',
    layout: 'hbox',
    idField: null,
    descField: null,
    
    /**
     * storeUrl: Caminho do arquivo de retorno.
     */
    handlerUrl: 'src/php/handler.php',
    
    /**
     * table: Nome da tabela que irá prover os dados para o campo.
     */
    table: null,
    
    /*
     * className: Nome da classe que irá prover os dados para o campo.
     */
    className: 'FDefaultController',
    
    /*
     * searchForm: Formulário de busca de registros
     */
    searchForm: null,
    
    initComponent: function() 
    {
        this.fieldCls = 'ftextfield';
        this.labelCls = 'ftextfieldlabel';
        this.readOnlyCls = 'readonly';
        this.requiredCls = 'required-field';
        
        this.fieldLabel = this.label ? this.label : this.fieldLabel;
        this.validateOnBlur = true;
        this.validateOnChange = true;
        
        this.descField = new FTextField({
            fieldWidth: this.fieldWidth,
            name: this.name + '_' + this.descFieldName,
            readOnly: true,
            disabled: true
        });
        
        this.idField = new FTextField({
            width: this.fieldWidth / 5,
            name: this.name,
            scope: this,
            handlerUrl: this.handlerUrl,
            table: this.table,
            className: this.className,
            dataIndex: this.dataIndex,
            descField: this.descField,
            descFieldName: this.descFieldName,
            onBlur: function()
            {
                if ( this.value )
                {
                    Ext.Ajax.request(
                    {
                        url: this.handlerUrl,
                        params: {
                            manager: this.className,
                            action: 'get',
                            table: this.table,
                            idxName: this.dataIndex,
                            dataId: this.value
                        },
                        scope: this,
                        success: function(response, opts)
                        {
                            var obj = Ext.decode(response.responseText);

                            if ( !obj.data[this.dataIndex] )
                            {
                                this.setValue(null);
                                this.descField.setValue(null);

                                Ext.create('FMessage', {
                                    msg: obj.msg
                                }).show();
                            }
                            else
                            {
                                this.descField.setValue(obj.data[this.descFieldName]);
                            }
                        },
                        failure: function(response, opts)
                        {
                            this.setValue(null);
                            this.descField.setValue(null);

                            var obj = Ext.decode(response.responseText);

                            Ext.create('FMessageError', {
                                msg: obj.msg
                            }).show();
                        }
                    });
                }
            },
            afterRender: function()
            {
            }
        });
       
        this.items = [
            this.idField,
            {
                xtype: 'splitter'
            },
            {
                xtype: 'fbutton',
                name: 'btnLkpSearch',
                width: 30,
                height: 26,
                border: 0,
                iconCls: 'fbuttonsearch-icon',
                handler: this._onBtnLkpSearchClick,
                scope: this
            },
            {
                xtype: 'splitter'
            },
            this.descField
        ];
    
        this.callParent();
    },
    _onBtnLkpSearchClick: function()
    {
        if ( this.searchForm != null )
        {
            Ext.create(this.searchForm, {
                idField: this.idField,
                descField: this.descField,
                idxName: this.dataIndex
            }).show();
        }
        else
        {
            Ext.create('FMessage', {
                msg: 'Não foi definido um formulário de busca.'
            }).show();
        }
    }
});
